<script setup lang="ts">
import { patientConsultOrderPre, patientInfo, patientConsultOrder } from '@/api/consult'
import { useConsultStore } from '@/stores'
import type { patientConsultOrderPreApi, patientInfoApi } from '@/types/consult'
import { showFailToast } from 'vant'
import { onMounted } from 'vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import CpPay from '@/components/CpPay.vue'
const router = useRouter()
const store = useConsultStore()
const payInfo = ref<patientConsultOrderPreApi>()
// 订单详情获取
const getData = async () => {
  const res = await patientConsultOrderPre({
    type: 2,
    illnessType: store.consultInfo?.illnessType
  })
  payInfo.value = res.data
  console.log('支付详情数据', res)
}
// 获取患者详情
const patientMoreInfo = ref<patientInfoApi>()
const getPatientInfo = async () => {
  const res = await patientInfo(store.consultInfo?.patientId!)
  patientMoreInfo.value = res.data
  console.log('患者详情', res)
}
getData()
getPatientInfo()

// 调用创建支付订单接口
const agree = ref(false)
const pay = ref<InstanceType<typeof CpPay>>()
const loading = ref(false)
const orderId = ref('')
const submit = async () => {
  loading.value = true
  const res = await patientConsultOrder(store.consultInfo!)
  loading.value = false
  orderId.value = res.data.id
  console.log('订单返回值', res.data.id)
  // 打开支付弹框
  pay.value!.show = true
}
/*
  vue3  ref
   1:定义响应式数据
   2：获取dom
      const imgDom=ref<>()   $0.__proto__
   3:获取组件实例
      const xxxCom=ref<InstanceType<typeof Xxx组件>>()
*/
// 打开页面时没有问诊订单请求参数，返回首页，再提示一下
onMounted(() => {
  if (!store.consultInfo) {
    showFailToast('暂无患者咨询数据，请重新选择')
    router.push('/')
  }
})

// 公共组件封装在vue+ts环境要怎么处理？
// 1：创建公共组件 2：定义公共组件的类型申明 3：直接可以使用（借助插件自动导入处理）
</script>

<template>
  <div class="consult-pay-page" v-if="payInfo">
    <cp-nav-bar title="支付" />
    <div class="pay-info">
      <p class="tit">图文问诊 {{ payInfo?.payment }} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>
    <van-cell-group>
      <van-cell title="优惠券" :value="`-¥${payInfo?.couponDeduction}`" />
      <van-cell title="积分抵扣" :value="`-¥${payInfo?.pointDeduction}`" />
      <van-cell title="实付款" :value="`¥${payInfo?.actualPayment}`" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>
    <van-cell-group>
      <van-cell
        title="患者信息"
        :value="`${patientMoreInfo?.name} | ${patientMoreInfo?.genderValue} | ${patientMoreInfo?.age}岁`"
      ></van-cell>
      <van-cell title="病情描述" :label="store.consultInfo?.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox v-model="agree">我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>
    <van-submit-bar
      :loading="loading"
      :disabled="!agree"
      button-type="primary"
      :price="payInfo?.actualPayment * 100"
      button-text="立即支付"
      text-align="left"
      @submit="submit"
    />
    <CpPay ref="pay" payCallback="/room" :orderId="orderId" :money="payInfo?.actualPayment"></CpPay>
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 46px 0 50px 0;
}
.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}
.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}
.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
</style>
